import numpy as np
from sklearn.decomposition import PCA
import warnings

from util import imageUtil

warnings.filterwarnings("ignore")


def _mean_variance_normalized(data):
    """
    均值方差归一化
    :param data:
    :return:
    """
    mu = np.average(data)
    sigma = np.std(data)
    x = (data - mu) / sigma
    return x


class LoadPCA:
    """
    PCA工具类
    """
    # 数据
    training = None

    def __init__(self, training):
        """
        构造方法
        :param training: 训练数据
        """
        self.training = np.array(training)

    def startPCA(self):
        # 构建模型,保留所有属性
        pca = PCA(n_components=2, copy=True, whiten=True)
        # 数据归一化
        self.training = _mean_variance_normalized(self.training)
        # 拟合数据
        pca.fit(self.training)
        # 做出三维图
        imageUtil.make3DImage(self.training)
        # print(pca.explained_variance_ratio_)
        # print(pca.explained_variance_)
        return pca
